﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Marta.buchalska.wesele
{
    public partial class EntryFormAdmin : System.Web.UI.Page
    {
        private TextBox _inserNewItemName;
        private DropDownList _insertNewItemType;

        protected void entryFormItemsSourceData_Inserting(object sender, LinqDataSourceInsertEventArgs e)
        {

            var newItem = (Marta.buchalska.wesele.DataModel.Entry_form_item)e.NewObject;
            newItem.Entry_form_tab_id = (int)TabListGridView.SelectedValue;
            newItem.Entry_form_item_type = int.Parse(_insertNewItemType.SelectedValue);

        }

        protected void InsertItemDetailView_Load(object sender, EventArgs e)
        {
            if (TabListGridView.SelectedValue != null)
                InsertItemDetailView.Visible = true;
        }

        protected void ItemNameTextBox_Init(object sender, EventArgs e)
        {
            _inserNewItemName = (TextBox)sender;
        }

        protected void InsertItemType_Load(object sender, EventArgs e)
        {
            _insertNewItemType = (DropDownList)sender;
        }

        protected void TabListGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            DataModel.WeedingDBDataContext context = new DataModel.WeedingDBDataContext();
            var toDelete = context.Entry_form_tabs.FirstOrDefault(d => d.ID == (int)e.Keys[0]);

            if (toDelete != null)
            {
                foreach (var item in toDelete.Entry_form_items)
                {
                    context.Entry_form_items.DeleteOnSubmit(item);
                }
                context.SubmitChanges();
            }
        }
    }
}